package msu.erdc.hci;

import java.util.Calendar;
import java.util.List;

import msu.erdc.hci.DateDialogFragment.DateDialogFragmentListener;
import msu.erdc.hci.customObjects.AccidentObject;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Accident extends Fragment{
	
	TextView coordinates;
	EditText street;
	EditText intersection;
	EditText city;
	EditText state;
	EditText comments;
	Button dateButton;
	Button reportButton;
	Button sketchButton;
	Button mediaButton;
	AccidentObject accident;
	int index;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.accident, container, false);
		final FragmentManager manager = getFragmentManager();
		
		coordinates = (TextView) view.findViewById(R.id.coordinateText);
		coordinates.setText(accident.latitude + "," + accident.longitude);
		
		street = (EditText) view.findViewById(R.id.streetText);
		street.setText(accident.street);
		
		intersection = (EditText) view.findViewById(R.id.intersectionText);
		intersection.setText(accident.nearestIntersection);
		
		city = (EditText) view.findViewById(R.id.cityText);
		city.setText(accident.city);
		
		state = (EditText) view.findViewById(R.id.stateText);
		state.setText(accident.state);
		
		comments = (EditText) view.findViewById(R.id.commentsText);
		comments.setText(accident.comments);
		
		dateButton = (Button) view.findViewById(R.id.dateButton);

		if(accident.day != 0 && accident.month != 0 && accident.year!= 0)
			dateButton.setText(accident.month + " / " + accident.day + " / " + accident.year);
		
		dateButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Calendar date = Calendar.getInstance();
				
				DateDialogFragment ddf = DateDialogFragment.newInstance(getActivity(), R.string.inspection_date_title, date, true);
				ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
				 
				    public void dateDialogFragmentDateSet(Calendar date) {
				    	
				    	accident.day = date.get(Calendar.DATE);
				    	accident.month = date.get(Calendar.MONTH) + 1;
				    	accident.year = date.get(Calendar.YEAR);
				    	
				        // update the fragment
				    	dateButton.setText(accident.month + " / " + accident.day + " / " + accident.year);
				    }
				});
				 
				ddf.show(getFragmentManager(), "date picker dialog fragment");
			}});
		
		reportButton = (Button) view.findViewById(R.id.reportButton);
		reportButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				ReportList report = new ReportList();
				Bundle arguments = new Bundle();
				arguments.putInt("accidentIndex", index);
				report.setArguments(arguments);
				ft.replace(android.R.id.content, report, "reportList");
				ft.addToBackStack(null);
				ft.commit();
				
			}});
		
		mediaButton = (Button) view.findViewById(R.id.mediaButton);
		mediaButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = manager.beginTransaction();
				Media media = new Media();
				Bundle arguments = new Bundle();
				arguments.putString("breadcrumbText", accident.toString());
				media.setArguments(arguments);
				ft.replace(android.R.id.content, media, "media");
				ft.addToBackStack(null);
				ft.commit();
			}});
		
		
		sketchButton = (Button) view.findViewById(R.id.sketchButton);
		sketchButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = manager.beginTransaction();
				Sketch sketch = new Sketch();
				Bundle arguments = new Bundle();
				arguments.putString("breadcrumbText", accident.toString());
				sketch.setArguments(arguments);
				ft.replace(android.R.id.content, sketch, "sketch");
				ft.addToBackStack(null);
				ft.commit();
			}});
		
		return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //do stuff here
        
        Bundle arguments = getArguments();
        if(!arguments.containsKey("accidentIndex"))
        {	
        	accident = new AccidentObject();
        	List<AccidentObject> accidentList = ((Global)getActivity().getApplication()).accidents;
        	index = accidentList.size()-1;
        }
        else
        {
        	index = arguments.getInt("accidentIndex");
        	accident = ((Global)getActivity().getApplication()).accidents.get(index);
        	
       	}
	}
}
